﻿<?php
include 'test_header.php';

class test_user_topic_process extends UnitTestCase{
	
	private $db ; 

	function setUp(){

		$this->db = db_connect();

		$sql = "
CREATE TABLE IF NOT EXISTS `topic` (
  `topic_no` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `problem_no` mediumint(9) NOT NULL,
  `title` varchar(1024) NOT NULL,
  `content` text NOT NULL,
  `username` varchar(256) NOT NULL,
  `view_count` int(11) DEFAULT '0',
  `reply_count` int(11) DEFAULT '0',
  `post_time` datetime NOT NULL,
  `hot` tinyint(1) NOT NULL DEFAULT '0',
  `lock` tinyint(1) DEFAULT NULL,
  UNIQUE KEY `topic_no` (`topic_no`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
		";

		$res = $this->db->query( $sql );

		$sql = "
INSERT INTO `topic` (`topic_no`, `problem_no`, `title`, `content`, `username`, `view_count`, `reply_count`, `post_time`, `hot`, `lock`) VALUES
(1, 21123, 'sad', 'asdads', 'anonymous', 0, 0, '2010-06-02 20:33:20', 0, NULL);
";

		$res = $this->db->query( $sql );
	}

	function tearDown(){
		
		$sql = "drop table topic";

		$res = $this->db->query( $sql );
	
	}
	function test_post_a_new_topic(){
	
		$request = array( "title"=>"test",
			          "problem_no"=>"1000",
				  "content"=>"testesttesteset",
				  "username"=>"tester"
			  	);

		$topic_process = new topic_process( $this->db , $request );

		$this->assertTrue( $topic_process->save() ) ;
	}
	function test_plus_topic_view_count(){

		$topic_process = new topic_process( $this->db );
		$this->assertTrue( $topic_process->load( 'topic_no' , 1 ) );
		$view_count = $topic_process->get( 'view_count' );
		$this->assertTrue( $topic_process->plus_view_count() );
		$this->assertEqual( $topic_process->get( 'view_count' ) , $view_count+1 );
	}
	
	function test_get_all_topic(){
		
		$topics = topic_process::get_all( $this->db );
		$this->assertTrue( is_array( $topics ) );
	}
}
	

$test = new test_user_topic_process();
$test->run( new HtmlReporter() );

